import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.Before;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

public class CalLogicTest {
	
	CalLogic cal;
	StoreInput input;

	@Before
	public void setUp() {
		cal = new CalLogic();
		input = new StoreInput();
	}
	
	@Test //Test priority is converted into a valid integer
	public void testPriority() {
		assertTrue("High priority is not a valid number", 
				(Integer.parseInt(cal.convertPriority("High")) >= 1) && (Integer.parseInt(cal.convertPriority("High")) <= 4));
		assertTrue("Medium priority is not a valid number", 
				Integer.parseInt(cal.convertPriority("Medium")) == 5);
		assertTrue("Low priority is not a valid number", 
				(Integer.parseInt(cal.convertPriority("Low")) >= 6) && (Integer.parseInt(cal.convertPriority("High")) <= 9));
	}

	@Test //Test valid date does not throw an exception
	public void testValidDate() {
		boolean thrown = false;
		try {
			cal.validDate("07/31/2014");
		} catch (Exception e){
			thrown = true;
		}
		assertFalse("Valid date has error", thrown);
	}
	
	@Test //Test invalid date throws an exception
	public void testInValidDate() {
		boolean thrown = false;
		try {
			cal.validDate("02/31/2014");
		} catch (Exception e){
			thrown = true;
		}
		assertTrue("Invalid date being accepted", thrown);
	}
	
	@Test //Test invalid date format throws an exception
	public void testInValidDateFormat() {
		boolean thrown = false;
		try {
			cal.validDate("07-31-2014");
		} catch (Exception e){
			thrown = true;
		}
		assertTrue("Invalid date format being accepted", thrown);
	}

	@Test //Test date is converted into expected format
	public void testConvertDate() {
		try {
			assertEquals("Invalid date conversion", cal.convertDate("8/10/2014"), "20140810");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test //Test the date comparison check for expected output
	public void testCompareDates() {
		try {
			assertEquals("Error when from > to", cal.compareDates("08102014", "08012014"), -1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			assertEquals("Error when from == to",cal.compareDates("08092014", "08102014"), 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			assertEquals("Error when from < to", cal.compareDates("08102014", "08102014"), 0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test //Test valid time does not throw an exception
	public void testValidTime() {
		boolean thrown = false;
		try {
			cal.validTime("11:49 am");
		} catch (Exception e){
			thrown = true;
		}
		assertFalse("Valid time has error", thrown);
	}
	
	@Test //Test invalid time throws an exception
	public void testInValidTime() {
		boolean thrown = false;
		try {
			cal.validTime("13:00 pm");
		} catch (Exception e){
			thrown = true;
		}
		assertTrue("Invalid time being accepted", thrown);
	}
	
	@Test //Test invalid time throws an exception
	public void testInValidTimeFormat() {
		boolean thrown = false;
		try {
			cal.validTime("99:99:99");
		} catch (Exception e){
			thrown = true;
		}
		assertTrue("Invalid time format being accepted", thrown);
	}
	
	@Test //Test time is converted into expected format
	public void testConvertTime() {
		try {
			assertEquals("Invalid time conversion", cal.convertTime("11:00 AM"), "110000");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test //Test the time comparison check for expected output
	public void testCompareTimes() {
		try {
			assertEquals("Error when from > to", cal.compareTimes("220000", "050000"), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			assertEquals("Error when from == to",cal.compareTimes("220000", "220000"), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			assertEquals("Error when from < to", cal.compareTimes("050000", "220000"), true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test //Test that the time zone is obtained correctly
	public void testGetTimeZone() {
		assertEquals("Getting time zone results in an unexpected timezone", cal.getTimeZone(), "Pacific/Honolulu"); 
	}

	@Test //Test latitude check is valid
	public void testLatitude() {

		try {
			assertEquals("Latitude check 1 is incorrect", cal.validLat("90"), true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			assertEquals("Latitude check 2 is incorrect", cal.validLat("90.00009"), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			assertEquals("Latitude check 3 is incorrect", cal.validLat("3424231"), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test //Test longitude check is valid
	public void testLongitude() {
		try {
			assertEquals("Longitude check 1 is incorrect", cal.validLng("180"), true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			assertEquals("Longitude check 2 is incorrect", cal.validLng("180.00009"), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			assertEquals("Longitude check 3 is incorrect", cal.validLng("3424231"), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test //Test ability to make valid ICS file
	public void testToICSFile_valid() {
		
			input.setFileName("Study for exam");
			input.setPrivacy("Public");
			input.setLocation("Hamilton Library");
			input.setLatitude("21.300927");
			input.setLongitude("-157.816213");
			input.setPriority("Low");
			input.setSummary("Gotta cram for that exam.");
			input.setFromDate("08/13/2014");
			input.setFromTime("09:00 AM");
			input.setToDate("08/13/2014");
			input.setToTime("12:00 PM");
			
			try {
				cal.toICSFile(input);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	@Test //Test inability to make ICS file with invalid date/time
	public void testToICSFile_invalidDateTime() {
		
		input.setFileName("INVALID");
		input.setPrivacy("Public");
		input.setLocation("Hamilton Library");
		input.setLatitude("21.300927");
		input.setLongitude("-157.816213");
		input.setPriority("Low");
		input.setSummary("Gotta cram for that exam.");
		input.setFromDate("08/13/2014");
		input.setFromTime("09:00 AM");
		input.setToDate("08/01/2014");
		input.setToTime("12:00 PM");
		
		try {
			cal.toICSFile(input);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	
}
